package com.example.messengerapp.ui

import android.content.Context
import android.os.Parcel
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.DataBindingUtil.getDefaultComponent
import androidx.databinding.ViewDataBinding
import com.example.messengerapp.R

abstract class ItemMessageBinding(
    root: View,
    val textViewMessage: TextView,
    val imageViewMessage: ImageView
) : ViewDataBinding(getDefaultComponent(), root, 0) {
    abstract override fun executeBindings()
    abstract override fun invalidateAll()
    abstract override fun hasPendingBindings(): Boolean
}

class ConcreteItemMessageBinding(
    root: View,
    textViewMessage: TextView,
    imageViewMessage: ImageView
) : ItemMessageBinding(root, textViewMessage, imageViewMessage), Parcelable {

    override fun executeBindings() {
        // Implement binding logic if needed
    }

    override fun setVariable(variableId: Int, value: Any?): Boolean {
        // Implement if needed
        return false
    }

    override fun onFieldChange(localFieldId: Int, `object`: Any?, fieldId: Int): Boolean {
        // Implement if needed
        return false
    }

    override fun invalidateAll() {
        // Implement if needed
    }

    override fun hasPendingBindings(): Boolean {
        // Implement if needed
        return false
    }

    // Parcelable implementation
    constructor(parcel: Parcel) : this(
        // Inflate the view from the context available in the parcel
        LayoutInflater.from(parcel.readContext()).inflate(R.layout.item_message, null, false),
        // Get the view IDs and retrieve the views from the inflated layout
        parcel.readTextView(parcel.readInt()),
        parcel.readImageView(parcel.readInt())
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(textViewMessage.id)
        parcel.writeInt(imageViewMessage.id)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<ConcreteItemMessageBinding> {
        override fun createFromParcel(parcel: Parcel): ConcreteItemMessageBinding {
            return ConcreteItemMessageBinding(parcel)
        }

        override fun newArray(size: Int): Array<ConcreteItemMessageBinding?> {
            return arrayOfNulls(size)
        }

        fun inflate(inflater: LayoutInflater, parent: ViewGroup?, attachToParent: Boolean): ConcreteItemMessageBinding {
            val view = inflater.inflate(R.layout.item_message, parent, attachToParent)
            val textViewMessage = view.findViewById<TextView>(R.id.textViewMessage)
            val imageViewMessage = view.findViewById<ImageView>(R.id.imageViewMessage)
            return ConcreteItemMessageBinding(view, textViewMessage, imageViewMessage)
        }
    }
}

// Extension function to retrieve the context from a Parcel
private fun Parcel.readContext(): Context {
    return Class.forName("android.app.ActivityThread")
        .getMethod("currentApplication")
        .invoke(null) as Context
}

// Extension function to retrieve a TextView from a view ID
private fun Parcel.readTextView(id: Int): TextView {
    val context = this.readContext()
    return LayoutInflater.from(context).inflate(R.layout.item_message, null, false).findViewById(id)
}

// Extension function to retrieve an ImageView from a view ID
private fun Parcel.readImageView(id: Int): ImageView {
    val context = this.readContext()
    return LayoutInflater.from(context).inflate(R.layout.item_message, null, false).findViewById(id)
}
